namespace Guia.Elements
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Locator;

    /// <summary>
    /// This class represents a TextBox.
    /// </summary>
    public class TextBox : UiElement
    {
        /// <summary>
        /// Initializes a new instance of the TextBox class. 
        /// </summary>
        /// <param name="automationElement">The AutomationElement representating this TextBox.</param>
        public TextBox(AutomationElement automationElement)
            : base(automationElement)
        {
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.Edit); }
        }

        /// <summary>
        /// Gets or sets the value of this TextBox.
        /// </summary>
        /// <exception cref="OperationNotSupportedException">A OperationNotSupportedException is thrown when the value can't be set (e.g. the text-box is readonly).</exception>
        /// <exception cref="ArgumentNullException">This exception is thrown if value is null.</exception>
        public virtual string Value
        {
            get
            {
                return GetPattern<ValuePattern>().Current.Value;
            }

            set
            {
                Contract.Requires<ArgumentNullException>(value != null);
                try
                {
                    GetPattern<ValuePattern>().SetValue(value);
                }
                catch (InvalidOperationException)
                {
                    throw new OperationNotSupportedException("The element is not enabled");
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether this TextBox is read-only or not.
        /// </summary>
        public bool IsReadOnly
        {
            get { return GetPattern<ValuePattern>().Current.IsReadOnly; }
        }
    }
}